package net.huashitong.user.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.filter.Paging;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.date.DateUtils;
import net.huashitong.user.dao.LoginLogDao;
import net.huashitong.user.databean.OnLineDatabean;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.entity.LoginLog;
import net.huashitong.user.filter.OnlineFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional
@Service
public class LoginLogService extends StringPKService<LoginLog> {
    @Autowired
    private SysTableService tableService;
    @Autowired
    private LoginLogDao dao;

    @Override
    protected BaseDao<LoginLog, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(LoginLog loginLog) {
        loginLog.setSysId(tableService.updateMaxSysId("t_bnp_login_log", null));
    }

    public boolean doLoginLog(BnpUser bnpUser) {
        LoginLog loginLog = new LoginLog();
        loginLog.setBranch(bnpUser.getBranch());
        loginLog.setBranchType(bnpUser.getBranchType());
        loginLog.setName(bnpUser.getName());
        loginLog.setPhone(bnpUser.getPhone());
        loginLog.setRole(bnpUser.getRole());
        LoginLog existLoginLog = dao.getLoginLogByCreateDateAndUser(DateUtils.getDate(), loginLog);
        if (ObjectUtils.isBlank(existLoginLog)) {
            save(loginLog);
            return true;
        } else {
            save(loginLog);
            return false;
        }
    }

    /**
     * 统计部门点击率
     * @return
     */
    public Map<String, Integer> queryDeptLoginCount() {
        return dao.queryDeptLoginCount();
    }

    /**
     * 统计用户点击率
     * @return
     */
    public Map<String, Integer> queryUserLoginCount() {
        return dao.queryUserLoginCount();
    }

    /**
     * 用户登录次数统计
     * @return
     */
    public List<OnLineDatabean> queryUserOnLineCount(OnlineFilter filter) {
        return dao.queryUserOnLineCount(filter);
    }

    /*public String queryOnlineCount(){
        return dao.queryOnlineCount();
    }*/

    public List<OnLineDatabean> queryUserOnlineList(OnlineFilter filter){
        /*if (filter.getPage() == null ){
            filter.setPage(0);
        }
        Paging p = new Paging();
        p.setCurrentPage(filter.getPage() + 1);
        filter.setPaging(p);*/
        return dao.queryUserOnlineList(filter);
    }

    public Map<String, Integer> queryLoginCount() {
        return dao.queryLoginCount();
    }


    public int queryUserOnlineListCount(){
        return dao.queryUserOnlineListCount();
    }
}
